package com.mark.web.data.controller.backend;

import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.data.entity.po.Product;
import com.mark.web.data.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 商品被【后台管理】 调用的接口
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */

@RestController
@RequestMapping("/manage/product/")
public class ProductManageController {
    @Autowired
    private IProductService iProductService;


    //1.保存商品（添加，更新）
    @PostMapping(value = "save_product.do")
    public ServerResponse productSave(Product product){
        int currentUserId = CurrentUser.getCurrentUserId();
        product.setOwnerId(currentUserId);
        return iProductService.insertOrUpdateProduct(product);
    }

    //2.修改产品销售状态
    @PostMapping(value = "set_sale_status.do")
    public ServerResponse setSaleStatus(Integer productId,Integer status){
        return  iProductService.setSaleStatus(productId,status);
    }

    //3.获取商品详情
    @PostMapping(value = "product_detail.do")
    public ServerResponse productDetail(Integer productId){
        return iProductService.getProductDetail(productId);
    }

    //4.商品按创建时间升序分页显示
    @PostMapping(value = "product_list.do")
    public ServerResponse getList(Integer ownerId,// TODO: 2018/8/14  后台调用（ownerId 不用传值）
                                  @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
        ownerId = null;
        return iProductService.getProductList(pageNum,pageSize,ownerId);
    }

    //5.商品搜索：（1：按商品名模糊搜索）（2：按Id搜索）（3：按状态搜索）
    @PostMapping(value = "search_product.do")
    public ServerResponse searchProduct(String productName,
                                        Integer productId,
                                        Integer status,
                                        @RequestParam(value = "pageNum",defaultValue ="1" ) int pageNum,
                                        @RequestParam(value = "pageSize",defaultValue ="10" )int pageSize){
        return iProductService.searchProduct(null,productName,productId,status,pageNum,pageSize);
    }


}
